home *** CD-ROM | disk | FTP | other *** search
- /*
- File: FWDictLi.cpp
-
- Contains: Implementation of FW_CPrivDictionaryList
-
- Written by: Richard Rodseth
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <3> 8/17/94 jpa Added DeleteSOMValues method to delete SOM
- objects properly [1181515]
- <2> 6/9/94 RR Remove ASLM Stuff. XMP->OD
- <2> 3/15/94 MB Changes to support SCpp/ASLM builds,
- #1150864.
- <3> 11/4/93 RR Moved class FW_CPrivAssociation from .h. Fixed
- memory leak caused by DeleteValues removing
- links so that they weren't there anymore
- for RemoveAll to delete.
- <2> 10/28/93 RR Removed ambiguous constructor
- <1> 8/16/93 RCR first checked in
-
- To Do:
- */
-
- #include "FWFound.hpp"
-
- #ifndef FWDICTLI_H
- #include "FWDictLi.h"
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwcollec
- #endif
-
-
- //=====================================================================================
- // Class FW_CPrivAssociation
- //=====================================================================================
-
- FW_CPrivAssociation::FW_CPrivAssociation(FW_PrivKeyType key, FW_PrivValueType value)
- {
- fKey = key;
- fValue = value;
- }
-
- FW_CPrivAssociation::~FW_CPrivAssociation()
- {
- }
-
- //======================================================================================
- // Class FW_CPrivDictionaryList
- //======================================================================================
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::FW_CPrivDictionaryList
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_CPrivDictionaryList::FW_CPrivDictionaryList()
- {
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::~FW_CPrivDictionaryList
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_CPrivDictionaryList::~FW_CPrivDictionaryList()
- {
- this->RemoveAll();
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::AddPair
- //
- // Description
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::AddPair(FW_PrivKeyType key, FW_PrivValueType value)
- {
- FW_Boolean found = FALSE;
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- while (association != NULL)
- {
- FW_PrivKeyType k = association->GetKey();
-
- if (this->KeysMatch(k,key))
- {
- found = TRUE;
- association->SetValue(value);
- break;
- }
- else
- association = (FW_CPrivAssociation*) iter.Next();
- }
- if (!found)
- {
- association = this->CreateNewAssociation(key,value);
- fImplementation.AddLast(association);
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::Count
- //
- // Description
- //------------------------------------------------------------------------------
-
- unsigned long FW_CPrivDictionaryList::Count()
- {
- return fImplementation.Count();
- }
-
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::ContainsKey
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivDictionaryList::ContainsKey(FW_PrivKeyType key)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- while (association != NULL)
- {
- FW_PrivKeyType k = association->GetKey();
-
- if (this->KeysMatch(k,key))
- {
- return TRUE;
- }
- else
- association = (FW_CPrivAssociation*) iter.Next();
- }
- return FALSE;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::ValueAtKey
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_PrivValueType FW_CPrivDictionaryList::ValueAtKey(FW_PrivKeyType key)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- while (association != NULL)
- {
- FW_PrivKeyType k = association->GetKey();
-
- if (this->KeysMatch(k,key))
- {
- return association->GetValue();
- }
- else
- association = (FW_CPrivAssociation*) iter.Next();
- }
- return NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::RemoveKey
- //
- // Description
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::RemoveKey(FW_PrivKeyType key)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- while (association != NULL)
- {
- FW_PrivKeyType k = association->GetKey();
-
- if (this->KeysMatch(k,key))
- {
- fImplementation.Remove(*association);
- break;
- }
- else
- association = (FW_CPrivAssociation*) iter.Next();
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::RemoveAll
- //
- // Description
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::RemoveAll()
- {
- FW_CPrivLink* link = fImplementation.RemoveFirst();
- while (link != NULL)
- {
- delete link;
- link = fImplementation.RemoveFirst();
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::DeleteKeys
- //
- // Description
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::DeleteKeys()
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
-
- for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- iter.IsNotComplete();
- association = (FW_CPrivAssociation*) iter.Next())
- {
- FW_PrivKeyType key = association->GetKey();
- delete key;
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::DeleteValues
- //
- // Delete non-class values; plain vanilla global operator delete will be called
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::DeleteValues()
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
-
- for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
- iter.IsNotComplete();
- association = (FW_CPrivAssociation*) iter.Next())
- {
- FW_PrivValueType value = association->GetValue();
- delete value;
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::CreateNewAssociation
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_CPrivAssociation* FW_CPrivDictionaryList::CreateNewAssociation(FW_PrivKeyType key, FW_PrivValueType value) const
- {
- return new FW_CPrivAssociation(key, value);
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::KeysMatch
- //
- // Description
- //------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivDictionaryList::KeysMatch(FW_PrivKeyType key1,FW_PrivKeyType key2) const
- {
- return (key1 == key2);
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivDictionaryList::Merge
- //------------------------------------------------------------------------------
-
- void FW_CPrivDictionaryList::Merge(FW_CPrivDictionaryList &other)
- {
- FW_CPrivLinkedListIterator *iterator = other.CreateIterator();
- for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iterator->First();
- iterator->IsNotComplete();
- association = (FW_CPrivAssociation*) iterator->Next())
- {
- if (!this->ContainsKey(association->GetKey()))
- this->AddPair(association->GetKey(), association->GetValue());
- }
- delete iterator;
- }
-